﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RooHui.OA.Biz;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Tool;
using System.Web.UI.WebControls;

namespace RooHui.OA.Web.Controllers
{
    [HandleError]
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 分页获取产品信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetProductListByPage(int page, int pageSize)
        {
            int count = 0;
            var productList = new List<Product>(); // = ProductBiz.GetProductListByPage(page, pageSize, ref count);
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Rows", productList);
            dic.Add("Total", count);
            return Content(dic.ToJson());
        }

        [HttpPost]
        public ActionResult GetProductListByKey(string key = "")
        {
            var productList = ProductBiz.GetProductList(key);
            productList.Insert(0, new Product() { ID = -1, ProductName = "全部" });

            return Content(productList.ToJson());
        }

        /// <summary>
        /// 获取启用状态的产品
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetEnabledProduct() 
        {
            var productlist = ProductBiz.GetProductByStatus(1);
            productlist.Insert(0, new Product() { ID = -1, ProductName = "请选择" });
            return Content(productlist.ToJson());
        }

        public ActionResult ProductAdd()
        {
            return View();
        }

        /// <summary>
        /// 增加产品
        /// </summary>
        /// <param name="productModel"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ProductAdd(string productName, string summary, string rateSummary)
        {
            bool bAddFlag = ProductBiz.ProductAdd(productName, summary, rateSummary);
            if (bAddFlag)
            {
                return Content("OK");
            }
            else
            {
                return Content("Failed");
            }
        }

        public ActionResult ProductEdit(int productId, int editType)
        {
            ViewBag.product = ProductBiz.GetProductByID(productId);
            ViewBag.editType = editType;
            return View();
        }

        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="productModel"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ProductEdit(int productId, string productName, string summary, string rateSummary)
        {
            bool bModifyFlag = ProductBiz.ProductEdit(productId, productName, summary, rateSummary);
            if (bModifyFlag)
            {
                return Content("OK");
            }
            else
            {
                return Content("Failed");
            }
        }

        [HttpPost]
        public ActionResult ProductStatusChange(int productId, int destinationStatus)
        {
            bool bChangeFlag = ProductBiz.ProductStatusChange(productId, destinationStatus);
            if (bChangeFlag)
            {
                return Content("OK");
            }
            else
            {
                return Content("Failed");
            }
        }
    }

}
